<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/12/7
 * Time: 10:26
 */

namespace api\storage\model;


use think\Collection;
use think\Db;
use think\Model;

class ReelsModel extends Model
{
    protected $table = 'iwh_reels';
    protected $pk = 'uuid';

    /**
     * 根据条件统计木某个物料的数量
     *
     * @param string $materielId
     * @param string $uuid
     * @return array
     */
    public function countMaterielNumber($materielId = '', $uuid)
    {
        $where['sub_materiel_id'] = $materielId;
        $where['storage_uuid'] = $uuid;
        $where['status'] = 1;
        return $this->where($where)->value('sum(count)');
    }

    /**
     * 查询物料号 和 仓库相同的物料
     *
     * @param $materielId
     * @param $uuid
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryByMaterielIdAndUuid($materielId, $uuid)
    {
        $where['status'] = 1;
        $where['storage_uuid'] = $uuid;
        $where['sub_materiel_id'] = $materielId;

        return $this->where($where)
            ->field('reel_id,seat_code,container_code,count')
            ->order('reel_id asc')
            ->select();
    }

    /***
     * 根据物料号和仓库推一条reelId，并对该行加锁堵塞。
     * @param $materielId
     * @param $storageUuid
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function findReelWithLock($materielId, $storageUuid)
    {
        $where['status'] = 1;
        $where['storage_uuid'] = $storageUuid;
        $where['sub_materiel_id'] = $materielId;

        $result = $this->where($where)
            ->order('time asc')
            ->lock(true)
            ->find();
        return $result;
    }

    /***
     * @param $materielId
     * @param $storageUuid
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 不加锁的查询
     */
    public function findReel($materielId, $storageUuid)
    {
        $where['status'] = 1;
        $where['storage_uuid'] = $storageUuid;
        $where['sub_materiel_id'] = $materielId;

        $result = $this->where($where)
            ->order('time asc')
            ->find();
        return $result;
    }

    /**
     * 根据容器号和 仓库uuid来查找数据
     *
     * @param $containerCode
     * @param $uuid
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryByContainerCodeAndUuid($containerCode, $uuid)
    {
        $where['container_code'] = $containerCode;
        $where['storage_uuid'] = $uuid;
        return $this->where($where)
            ->field('container_code,sub_materiel_id,reel_id,count')
            ->order('reel_id asc')
            ->select();
    }

    /**
     * 根据reelId 和 仓库uuid 获取数据
     *
     * @param $reelId
     * @param $uuid
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findReelsByIdAndUuid($reelId, $uuid)
    {
        //TODO:liuwenbin
        $where['reel_id'] = $reelId;
        $where['storage_uuid'] = $uuid;
        $result = $this->alias('r')->field('p.*,r.status')
            ->join(['iwh_purchase_detail' => 'p'], "p.reel_id = r.reel_id and storage_uuid = '$uuid'", 'right')
            ->where('p.reel_id', $reelId)
            ->find();
        return $result;
    }

    /**
     * 在特定仓库中寻找某种材料
     *
     * @param string $status 1为入库，0为出库
     * @param string $storage_uuid 仓库uuid
     * @param string $subMaterielId 材料uuid
     * @return false|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getReels($status = '', $storage_uuid = '', $subMaterielId = '')
    {
        $where = ['status' => $status, 'storage_uuid' => $storage_uuid, 'sub_materiel_id' => $subMaterielId];
        $field = 'reel_id,seat_code,container_code,count';
        $result = $this->where($where)->field($field)->order('reel_id asc')->select();
        return $result;
    }

    /**
     * 获取出库物料信息
     *
     * @param $ids
     * @param $uuid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryReelsByMaterielIds($ids, $uuid)
    {
        $where['status'] = 1;
        $where['storage_uuid'] = $uuid;
        $reels = Db::table($this->table)->where($where)
            ->whereIn('sub_materiel_id', $ids)
            ->field('reel_id,sub_materiel_id,count,seat_code,container_code')
            ->order('sub_materiel_id asc,reel_id asc')
            ->select();
        return count($reels) ? $reels->toArray() : [];
    }

    /**
     * 根据reelId和 仓库查找数据
     *
     * @param $reelId
     * @param $storageUuid
     * @param $status
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function findReelByIdAndUuid($reelId, $storageUuid, $status = 1)
    {
        return $this->where('reel_id', $reelId)
            ->where('storage_uuid', $storageUuid)
            ->where('status', $status)
            ->find();
    }

    public function queryReelsByIdAndUuid($reelIds, $storageUuid)
    {
        if (empty($reelIds)) {
            return new Collection();
        }
        return $this->where('storage_uuid', $storageUuid)
            ->whereIn('reel_id', $reelIds)
            ->select();
    }

    public function findByIdAndUuidWithLock($reelId, $storageUuid)
    {
        $where['reel_id'] = $reelId;
        $where['storage_uuid'] = $storageUuid;
        return $this->lock(true)->where($where)->find();
    }

    /**
     * 添加数据
     *
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        return $this->save($data);
    }

    /**
     * 批量添加
     *
     * @param $dataList
     * @return array|false
     * @throws \Exception
     */
    public function addAll($dataList)
    {
        return $this->saveAll($dataList);
    }

    /**
     * 更新数据
     *
     * @param $reelId
     * @param $storageUuid
     * @param $data
     * @return false|int
     */
    public function updateReel($reelId, $storageUuid, $data)
    {
        $where['reel_id'] = $reelId;
        $where['storage_uuid'] = $storageUuid;
        $result = $this->where($where)->update($data);
        return $result;
    }

    /**
     * 根据uuidList删除数据
     *
     * @param $uuidList
     * @return int
     */
    public function deleteByUuidList($uuidList)
    {
        if (is_string($uuidList)) {
            $uuidList = [$uuidList];
        }
        return $this->whereIn('uuid', $uuidList)->delete();
    }




    /**
     * 查询reelInfo
     *
     * @param $containerCode
     * @param $storageUuid
     * @return false|\PDOStatement|string|Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryReelsInfo($containerCode, $storageUuid)
    {
        return $this->where('container_code', $containerCode)
            ->where('storage_uuid', $storageUuid)
            ->select();
    }

    /**
     * 查找容器
     *
     * @param $seatCode
     * @param $storageUuid
     * @return array
     */
    public function queryContainers($seatCode, $storageUuid)
    {
        return $this->distinct(true)
            ->where('seat_code', $seatCode)
            ->where('storage_uuid', $storageUuid)
            ->order('container_code asc')
            ->column('container_code');
    }

    /**
     * 查询一条记录
     *
     * @param $materielId
     * @param $storageUuid
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findOneReelIdByMaterielIdAndUuid($materielId, $storageUuid)
    {
        return $this->where('sub_materiel_id', $materielId)
            ->where('storage_uuid', $storageUuid)
            ->find();
    }
    /*
     *
     *  #######################以下五个查询是用于盘点查询#####################
     *
     */

    /**
     * 根据货架模糊查询
     *
     * @param $shelfCode
     * @param $storageUuid
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryByShelfCodeWithSeat($shelfCode, $storageUuid)
    {
        $where = [
            'a.status' => 1,
            'a.seat_code' => ['like', $shelfCode . '%'],
            'a.storage_uuid' => $storageUuid,
            'b.storage_uuid' => $storageUuid,
            'b.seat_code' => ['like', $shelfCode . '%'],
        ];
        return $this->alias('a')
            ->field('a.uuid,a.reel_id as reelId,a.sub_materiel_id as subMaterielId,a.count,a.seat_code as seatCode,b.seat_id as seatId')
            ->join(['iwh_seat' => 'b'], 'a.seat_code = b.seat_code', 'inner')
            ->order('a.seat_code asc')
            ->where($where)
            ->select();
    }

    /**
     * 查找没有关联的物料信息
     *
     * @param $shelfCode
     * @param $storageUuid
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryByShelfCode($shelfCode, $storageUuid = 1)
    {
        $where = [
            'status' => 1,
            'seat_code' => ['like', $shelfCode . '%'],
            'storage_uuid' => $storageUuid
        ];
        return $this->field('uuid,reel_id as reelId,seat_code as seatCode,count,sub_materiel_id as subMaterielId')
            ->order('seat_code asc')
            ->where($where)
            ->select();
    }

    /**
     * 根据容器来查询
     *
     * @param $containerCode
     * @param $storageUuid
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryByContainer($containerCode, $storageUuid)
    {
        $where = [
            'storage_uuid' => $storageUuid,
            'container_code' => $containerCode,
            'status' => 1
        ];
        return $this->field('uuid,reel_id as reelId,count,seat_code as seatCode,sub_materiel_id as subMaterielId')
            ->where($where)
            ->order('reel_id asc')
            ->select();
    }

    public function queryByMaterielIdWithSeat($materielId, $storageUuid)
    {
        $where = [
            'a.status' => 1,
            'a.sub_materiel_id' => $materielId,
            'a.storage_uuid' => $storageUuid,
            'b.storage_uuid' => $storageUuid
        ];

        return $this->alias('a')
            ->field('a.seat_code as seatCode,a.reel_id as reelId,a.sub_materiel_id as subMaterielId,a.count,a.container_code as containerCode,a.uuid,b.seat_id as seatId')
            ->join(['iwh_seat' => 'b'], 'a.seat_code = b.seat_code', 'inner')
            ->order('b.seat_code asc')
            ->where($where)
            ->select();
    }

    public function queryByMaterielId($materielId, $storageUuid)
    {
        $where = [
            'status' => 1,
            'sub_materiel_id' => $materielId,
            'storage_uuid' => $storageUuid,
        ];

        return $this->field('uuid,reel_id as reelId,sub_materiel_id as subMaterielId,container_code as containerCode,seat_code as seatCode,count')
            ->order('seat_code asc')
            ->where($where)
            ->select();
    }

    public function deleteByReelId($where){
        $result = $this->where($where)->delete();
        return $result;
    }

    /***
     * @param $data
     * @return false|\PDOStatement|string|Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException 获取货架中该物料的数量
     */
    public function getMaterielNumber($data){
        $where['status'] = 1;
        $where['storage_uuid'] = "184928afb9e35f3ff421da05fc3f917f";//smt仓
        $result = $this->whereIn('sub_materiel_id',$data)
            ->where($where)
            ->field('count(*) count,sub_materiel_id')
            ->group('sub_materiel_id')
            ->select();
        return $result;
    }

}